package com.kzj.global.override;

import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StreamUtils;

import javax.servlet.ReadListener;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

@Slf4j
public class MyHttpServletRequestWrapper extends HttpServletRequestWrapper {
    private byte[] bytes=null;
    public MyHttpServletRequestWrapper(HttpServletRequest request)throws IOException {
        super(request);
    }

    @Override
    public ServletInputStream getInputStream() throws IOException {
        if(bytes==null)
        {
            bytes= StreamUtils.copyToByteArray(super.getRequest().getInputStream());
        }
        log.info("bytes:\n"+
                new String(bytes));
        return new ServletInputStream() {
            ByteArrayInputStream byteArrayInputStream=new ByteArrayInputStream(bytes);
            @Override
            public boolean isFinished() {
                return false;
            }

            @Override
            public boolean isReady() {
                return false;
            }

            @Override
            public void setReadListener(ReadListener readListener) {

            }

            @Override
            public int read() throws IOException {
                return byteArrayInputStream.read();
            }
        };
    }

    @Override
    public BufferedReader getReader() throws IOException {
        if(bytes==null)
            bytes = StreamUtils.copyToByteArray(super.getInputStream());
        return new BufferedReader(new InputStreamReader(new ByteArrayInputStream(bytes)));
    }
}
